using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace CPRMulti.Tools
{
    public class OperationXML
    {
        delegate void de();
        private OperationXML()
        {
            
        }

        /// <summary>
        /// 加载XML并返回根节点
        /// </summary>
        /// <param name="path">XML文件路径</param>
        /// <returns></returns>
        public static XmlNode LoadXml(string path)
        {
            XmlNode xmlNode = null;

            XmlDocument xmlDocument = new XmlDocument();
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.IgnoreComments = true;

            try
            {              
                XmlReader reader = XmlReader.Create(path, settings);
                xmlDocument.Load(reader);
                xmlNode = xmlDocument?.DocumentElement;
            }
            catch (Exception e)
            {
                UnityEngine.Debug.LogError(e);
            }

            return xmlNode;
        }

        /// <summary>
        /// 加载XML并返回节点
        /// </summary>
        /// <param name="path">XML文件路径</param>
        /// <param name="nodeName">节点名称</param>
        /// <returns></returns>
        public static XmlNode LoadXml(string path, string nodeName)
        {
            XmlNode xmlNode = LoadXml(path);
            try
            {
                xmlNode = xmlNode?.SelectSingleNode(nodeName);
            }
            catch (Exception e)
            {
                UnityEngine.Debug.LogError(e);
            }
            return xmlNode;
        }

        /// <summary>
        /// XML反序列化
        /// </summary>
        /// <param name="xml">XML字符串文本</param>
        /// <param name="type">对象类型</param>
        /// <returns></returns>
        public static object FromXml(string xml, Type type)
        {
            object obj = null;
            if (string.IsNullOrEmpty(xml)) return null;

            using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
            {
                XmlSerializer serializer = new XmlSerializer(type);
                try
                {
                    obj = serializer.Deserialize(stream);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex);
                }
            }
            return obj;
        }

        /// <summary>
        /// XML反序列化
        /// </summary>
        /// <param name="filePath">XML路径</param>
        /// <param name="type">对象类型</param>
        /// <returns></returns>
        public static object FromXmlString(string filePath, Type type)
        {
            object obj = null;
            if (!File.Exists(filePath)) return null;

            string xmlString = File.ReadAllText(filePath);

            if (string.IsNullOrEmpty(xmlString)) return null;

            using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(xmlString)))
            {
                XmlSerializer serializer = new XmlSerializer(type);
                try
                {
                    obj = serializer.Deserialize(stream);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex);
                }
            }
            return obj;
        }
    }

}
